package edu.pablocor.AndNote.adapters;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import edu.pablocor.AndNote.MenuComprasActivity;
import edu.pablocor.AndNote.R;
import edu.pablocor.AndNote.database.DbConstants;

public class ListaComprasAdapter extends SimpleCursorAdapter {

	private final MenuComprasActivity mca;
	private LayoutInflater li;
	private int l;

	
	public ListaComprasAdapter(Context context, int layout, Cursor c,
			String[] from, int[] to) {
		super(context, layout, c, from, to);
		li = LayoutInflater.from(context);
		l = layout;
		mca = (MenuComprasActivity) context;
	}


	@Override
    public void bindView(View view, Context context, Cursor cursor) {
      TextView nombre = (TextView) view.findViewById(R.id.layoutcomprasTextView);
      nombre.setText(cursor.getString(cursor.getColumnIndex(DbConstants.NOMBRECOMPRA)));
      TextView fecha = (TextView) view.findViewById(R.id.layoutcomprasTextViewFecha);
      fecha.setText(cursor.getString(cursor.getColumnIndex(DbConstants.FECHACOMPRA)));
      
      Button cerrar = (Button) view.findViewById(R.id.layoutcompraseliminarboton);
      
      final int id = cursor.getInt(cursor.getColumnIndex(DbConstants.ID));
      final Cursor c = cursor;

      cerrar.setOnClickListener(new OnClickListener() {
		
		public void onClick(View v) {
			mca.deleteCompra(id, c);
		}
	});
      
    }
	
	
	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		final View view = li.inflate(l, parent, false);
        return view;
	}

}
